package com.liusha.quicksand.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.liusha.quicksand.NotFoundException;
import com.liusha.quicksand.po.User;
import com.liusha.quicksand.service.UserService;
import org.hibernate.annotations.common.util.impl.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 一世长安一世欢
 * @Date: 2020/10/17
 * @Time: 13:04
 * 查询所有的符合条件的用户信息，并分页传递给界面
 * @Description: No Description
 */

@Controller
public class RegisterController {

    private static final String REDIRECT_LIST = "redirect:/admin/users";

    @Autowired
    private UserService userService;

    @RequestMapping("/admin/users")
    public String lists(@PageableDefault(size = 20) String kw, Model model, Pageable pageable) {

        model.addAttribute("kw",kw);
        if (kw != null) kw = "%" + kw + "%";
        if (kw == null) kw = "%%";
        Page<User> pup = userService.findAll(kw, pageable);
        model.addAttribute("pages", pup);

        return "admin/users";
    }

    /* 网站注册页 */
    @GetMapping({"/registered", "/registered/{userId}"})
    public String registered(@PathVariable(name = "userId", required = false) Long userId, Model model) {

        User u = new User();
        if (userId != null && userId > 0) {
            u = userService.findById(userId);
        }

        model.addAttribute("user", u);

        return "registered";
    }

    @PostMapping("/saveuser")
    @Validated
    public String save(User user, BindingResult result,RedirectAttributes attributes) {

        try {
            if (result.hasErrors()) {
                attributes.addFlashAttribute("message", "该账号已被注册！！！");
                return "redirect:/registered";
            }
            if (user.getUserId() != null && user.getUserId() > 0) {
                User u = userService.findById(user.getUserId());
                user.setPassword(u.getPassword());
            }
            userService.save(user);
            attributes.addFlashAttribute("message", "注册成功！！！");
            return "redirect:/login";
        } catch (Exception no) {
            attributes.addFlashAttribute("message", "该账号已被注册！！！");
            return "redirect:/registered";
        }
    }

    @GetMapping("deleteuser/{userId}")
    public String delete(@PathVariable("userId") Long userId) {

        userService.deleteById(userId);

        return REDIRECT_LIST;
    }

    @GetMapping("/validuser/{userId}")
    public String validuser(@PathVariable("userId") Long userId) {

        User user = userService.findById(userId);
//        if (user.getValidstate()==0)

        user.setValidstate(1 - user.getValidstate());

        return REDIRECT_LIST;
    }
}
